﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CUiMFVariationsWithPlacementOriginDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using EngineeringInternalExtension;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui48;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CUiMFVariationsWithPlacementOriginDlg : TxForm
{
  private int m_configurationId;
  private IContainer components;
  private TxFlexGrid m_variationsList;
  private Button m_closeButton;

  internal CUiMFVariationsWithPlacementOriginDlg(int configurationId)
  {
    this.InitializeComponent();
    this.m_configurationId = configurationId;
  }

  private void CUiMFVariationsWithPlacementOriginDlg_Load(object sender, EventArgs e)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    this.CaluclateVariationsWithPlacementPoints();
    Cursor.Current = current;
  }

  private void CaluclateVariationsWithPlacementPoints()
  {
    ArrayList arrayList1 = new ArrayList();
    ArrayList arrayList2 = new ArrayList();
    if (this.m_configurationId == 0)
      return;
    ArrayList variationsPath = this.CaluclateVariationsWithPlacementOrigins();
    ArrayList protoList = new ArrayList();
    CApMFAdminUtils.GetPrototypesWithPlacementPoints((TxPlanningObject) new TxEmsGlobalServicesProvider().GetObjectByInternalId(this.m_configurationId), ref protoList);
    this.RemoveVariationsWithOnePlacementOrigin(ref variationsPath, protoList);
    if (((RowColCollection) ((C1FlexGridBase) this.m_variationsList).Rows).Count < variationsPath.Count + 1)
      ((RowColCollection) ((C1FlexGridBase) this.m_variationsList).Rows).Count = variationsPath.Count + 1;
    for (int index = 0; index < variationsPath.Count; ++index)
    {
      ArrayList list;
      if (((ArrayList) variationsPath[index])[0] is ArrayList)
      {
        list = ((ArrayList) variationsPath[index])[0] as ArrayList;
        ((ArrayList) variationsPath[index]).RemoveAt(0);
      }
      else
        list = new ArrayList();
      string withCommasFromArray = this.CreateStringWithCommasFromArray(variationsPath[index] as ArrayList);
      if (list.Count > 0 || protoList.Count > 0)
      {
        string str = "";
        if (list.Count > 0)
          str = this.CreateStringWithCommasFromArray(list);
        if (protoList.Count > 0)
        {
          if (str != "")
            str += ",";
          str += this.CreateStringWithCommasFromArray(protoList);
        }
        ((C1FlexGridBase) this.m_variationsList).Rows[index + 1][1] = (object) str;
      }
      ((C1FlexGridBase) this.m_variationsList).Rows[index + 1][0] = (object) withCommasFromArray;
      ((C1FlexGridBase) this.m_variationsList).Rows[index + 1][2] = (object) (list.Count + protoList.Count);
    }
  }

  private string CreateStringWithCommasFromArray(ArrayList list)
  {
    TxEmsGlobalServicesProvider servicesProvider = new TxEmsGlobalServicesProvider();
    string withCommasFromArray = "";
    for (int index = 0; index < list.Count; ++index)
    {
      ITxPlanningObject objectByInternalId = servicesProvider.GetObjectByInternalId((int) list[index]);
      withCommasFromArray = withCommasFromArray + TxObjectEx.GetPlanningObjectName((ITxObject) objectByInternalId) + ",";
    }
    if (withCommasFromArray.Length > 0)
      withCommasFromArray = withCommasFromArray.Substring(0, withCommasFromArray.Length - 1);
    return withCommasFromArray;
  }

  private void RemoveVariationsWithOnePlacementOrigin(
    ref ArrayList variationsPath,
    ArrayList protoList)
  {
    for (int index = 0; index < variationsPath.Count; ++index)
    {
      if (((ArrayList) variationsPath[index])[0] is ArrayList)
      {
        if (((ArrayList) ((ArrayList) variationsPath[index])[0]).Count + protoList.Count == 1)
        {
          variationsPath.RemoveAt(index);
          --index;
        }
      }
      else if (protoList.Count == 1)
      {
        variationsPath.RemoveAt(index);
        --index;
      }
    }
  }

  private ArrayList CaluclateVariationsWithPlacementOrigins()
  {
    CApMFFixtureDesignerApplication designerApplication = new CApMFFixtureDesignerApplication();
    new CApMFFixtureDesignerEmsHandler().ImportDataToCache(this.m_configurationId);
    designerApplication.ShouldCollectPlacementOrigins = true;
    TxObjectList field = new TxEmsGlobalServicesProvider().GetObjectByInternalId(this.m_configurationId).GetField("children") as TxObjectList;
    ArrayList resourcePath = new ArrayList();
    ArrayList variations = new ArrayList();
    Hashtable variationHash = new Hashtable();
    foreach (TxPlanningObject instancePlanningObj in (Collection<ITxObject>) field)
      designerApplication.CaluclateAllPossibleVariations((ITxPlanningObject) instancePlanningObj, ref variations, 0, resourcePath, true, variationHash);
    designerApplication.ShouldCollectPlacementOrigins = false;
    return variations;
  }

  private void m_closeButton_Click(object sender, EventArgs e) => ((Form) this).Close();

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiMFVariationsWithPlacementOriginDlg));
    this.m_variationsList = new TxFlexGrid();
    this.m_closeButton = new Button();
    ((ISupportInitialize) this.m_variationsList).BeginInit();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.m_variationsList, "m_variationsList");
    ((C1FlexGridBase) this.m_variationsList).AutoClipboard = true;
    ((C1FlexGridBase) this.m_variationsList).ExtendLastCol = true;
    ((C1FlexGridBase) this.m_variationsList).KeyActionTab = (KeyActionEnum) 2;
    ((Control) this.m_variationsList).Name = "m_variationsList";
    ((RowColCollection) ((C1FlexGridBase) this.m_variationsList).Rows).Count = 12;
    componentResourceManager.ApplyResources((object) this.m_closeButton, "m_closeButton");
    this.m_closeButton.Name = "m_closeButton";
    this.m_closeButton.UseVisualStyleBackColor = true;
    this.m_closeButton.Click += new EventHandler(this.m_closeButton_Click);
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Control) this).Controls.Add((Control) this.m_closeButton);
    ((Control) this).Controls.Add((Control) this.m_variationsList);
    ((Control) this).Name = nameof (CUiMFVariationsWithPlacementOriginDlg);
    ((Form) this).ShowIcon = false;
    ((Form) this).Load += new EventHandler(this.CUiMFVariationsWithPlacementOriginDlg_Load);
    ((ISupportInitialize) this.m_variationsList).EndInit();
    ((Control) this).ResumeLayout(false);
  }
}
